namespace Backend.Data.BackendDataContextMigrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class GeoReferenciaOrganigrama : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.GeoReferenciaOrganigramas",
                c => new
                    {
                        Id = c.Guid(nullable: false, identity: true),
                        GeoReferenciaId = c.Guid(nullable: false),
                        OrganigramaId = c.Guid(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.GeoReferencias", t => t.GeoReferenciaId)
                .ForeignKey("dbo.Organigramas", t => t.OrganigramaId)
                .Index(t => t.GeoReferenciaId)
                .Index(t => t.OrganigramaId);
            
            CreateTable(
                "dbo.GeoReferencias",
                c => new
                    {
                        Id = c.Guid(nullable: false, identity: true),
                        ParentId = c.Guid(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.LegalEntitys", t => t.Id)
                .ForeignKey("dbo.GeoReferencias", t => t.ParentId)
                .Index(t => t.Id)
                .Index(t => t.ParentId);
            
            CreateTable(
                "dbo.LegalEntitys",
                c => new
                    {
                        Id = c.Guid(nullable: false, identity: true),
                        Code = c.String(),
                        LegalName = c.String(),
                        Logo = c.String(),
                        Name = c.String(nullable: false),
                        Alias = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Ubicaciones",
                c => new
                    {
                        Id = c.Guid(nullable: false, identity: true),
                        CallePasajeMz = c.String(),
                        Latitud = c.String(),
                        Longitud = c.String(),
                        NroLote = c.String(),
                        Referencia = c.String(),
                        Sector = c.String(),
                        FullAddress = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.GeoReferencias", t => t.Id)
                .Index(t => t.Id);
            
            CreateTable(
                "dbo.Organigramas",
                c => new
                    {
                        Id = c.Guid(nullable: false, identity: true),
                        Alias = c.String(),
                        Name = c.String(nullable: false),
                        ParentId = c.Guid(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Organigramas", t => t.ParentId)
                .Index(t => t.ParentId);
            
            CreateTable(
                "dbo.AspNetUserGeoReferencias",
                c => new
                    {
                        UserId = c.String(nullable: false, maxLength: 128),
                        GeoReferenciaId = c.Guid(nullable: false),
                    })
                .PrimaryKey(t => new { t.UserId, t.GeoReferenciaId })
                .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
                .ForeignKey("dbo.GeoReferencias", t => t.GeoReferenciaId, cascadeDelete: true)
                .Index(t => t.UserId)
                .Index(t => t.GeoReferenciaId);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.GeoReferenciaOrganigramas", "OrganigramaId", "dbo.Organigramas");
            DropForeignKey("dbo.Organigramas", "ParentId", "dbo.Organigramas");
            DropForeignKey("dbo.GeoReferenciaOrganigramas", "GeoReferenciaId", "dbo.GeoReferencias");
            DropForeignKey("dbo.Ubicaciones", "Id", "dbo.GeoReferencias");
            DropForeignKey("dbo.GeoReferencias", "ParentId", "dbo.GeoReferencias");
            DropForeignKey("dbo.GeoReferencias", "Id", "dbo.LegalEntitys");
            DropForeignKey("dbo.AspNetUserGeoReferencias", "GeoReferenciaId", "dbo.GeoReferencias");
            DropForeignKey("dbo.AspNetUserGeoReferencias", "UserId", "dbo.AspNetUsers");
            DropIndex("dbo.AspNetUserGeoReferencias", new[] { "GeoReferenciaId" });
            DropIndex("dbo.AspNetUserGeoReferencias", new[] { "UserId" });
            DropIndex("dbo.Organigramas", new[] { "ParentId" });
            DropIndex("dbo.Ubicaciones", new[] { "Id" });
            DropIndex("dbo.GeoReferencias", new[] { "ParentId" });
            DropIndex("dbo.GeoReferencias", new[] { "Id" });
            DropIndex("dbo.GeoReferenciaOrganigramas", new[] { "OrganigramaId" });
            DropIndex("dbo.GeoReferenciaOrganigramas", new[] { "GeoReferenciaId" });
            DropTable("dbo.AspNetUserGeoReferencias");
            DropTable("dbo.Organigramas");
            DropTable("dbo.Ubicaciones");
            DropTable("dbo.LegalEntitys");
            DropTable("dbo.GeoReferencias");
            DropTable("dbo.GeoReferenciaOrganigramas");
        }
    }
}
